package com.techwells.learningkites.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.R.integer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.techwells.learningkites.R;
import com.techwells.learningkites.adapter.CourseExpandListAdapter;
import com.techwells.learningkites.adapter.MySentenceListAdapter;
import com.techwells.learningkites.model.MainPageViewModel;
import com.techwells.learningkites.model.MySentenceViewModel;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.learningkites.networkservice.model.Sentences;
import com.techwells.taco.base.CommonBaseActivity;
import com.techwells.taco.networkservice.ServiceMediator;
import com.techwells.taco.tasktool.TaskToken;
import com.techwells.taco.utils.ToastUtils;
import com.techwells.taco.widget.DelSlideListView;
import com.techwells.taco.widget.OnDeleteListioner;

public class MySententceActivity extends CommonBaseActivity implements OnDeleteListioner {
	
	private DelSlideListView lvMysentence;
	private List<Sentences> list;
	private MySentenceListAdapter adapter;
	
	private MySentenceViewModel presentViewModel;
	private List<Sentences> sentenceList;
	private int deletePosition;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mysententce);
		
//		initTitleBar(R.string.my_sentence, defaultLeftClickListener);
		initViews();
	}

	private void initViews() {
		lvMysentence = (DelSlideListView) findViewById(R.id.lv_mysentence);
//		list = new ArrayList<Sentences>();
//		for (int i = 0; i < 6; i++) {
//			Sentences s = new Sentences();
//			list.add(s);
//		}
//		adapter = new MySentenceListAdapter(this, list);
//		lvMysentence.setAdapter(adapter);
//		lvMysentence.setDeleteListioner(this);
//		lvMysentence.setOnItemClickListener(this);
//		adapter.setOnDeleteListioner(this);
	}

	@Override
	public boolean isCandelete(int position) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onDelete(int ID) {
//		list.remove(ID);
//		lvMysentence.deleteItem();
//		adapter.notifyDataSetChanged();
		if (sentenceList.size() > 0) {
			deletePosition = ID;
			doRequestDeleteSentence(sentenceList.get(ID).id);
		}
		
		
	}


	@Override
	public void onBack() {
		// TODO Auto-generated method stub
		
	}

	
	@Override
	public void alreadyBindBaseViewModel() {
		// TODO Auto-generated method stub
		super.alreadyBindBaseViewModel();
		
		presentViewModel = (MySentenceViewModel) baseViewModel;
	}

	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		doRequestGetSentences();
	}

	private void doRequestGetSentences() {
		doTask(KitesServiceMediator.SERVICE_GET_SENTENCES,null);
		
	}
	
	
	private void doRequestDeleteSentence(int videoSentenceId) {
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("videoSentenceId", videoSentenceId);
		doTask(KitesServiceMediator.SERVICE_DELETE_VIDEO_SENTENCE,argMap);
		
	}
	
	
	@Override
	public void refreshData(TaskToken token) {
		// TODO Auto-generated method stub
		super.refreshData(token);
		
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_SENTENCES)) {
			sentenceList = ((MySentenceViewModel)baseViewModel).sentenceList;
			adapter = new MySentenceListAdapter(this, sentenceList);
			lvMysentence.setAdapter(adapter);
			lvMysentence.setDeleteListioner(this);
			adapter.setOnDeleteListioner(this);
			
			initTitleBar(getResources().getString(R.string.my_sentence) + "( "+sentenceList.size() + " )", defaultLeftClickListener);
		}else if (method.equals(KitesServiceMediator.SERVICE_DELETE_VIDEO_SENTENCE)) {
			int returnCode = ((MySentenceViewModel)baseViewModel).returnCode;
			if (returnCode == ServiceMediator.Service_Return_Success) {
				sentenceList.remove(deletePosition);
				lvMysentence.deleteItem();
				adapter.notifyDataSetChanged();
				ToastUtils.show(this, "删除成功");
			}
		}
		
	}
}
